<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="lib/react/react.development.js"></script>
    <script src="lib/react/react-dom.development.js"></script>
    <script src="lib/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">
      let message = "前端小溪";
      class Demos extends React.Component {
        say() {
          console.log("这是demos");
        }
        render() {
          return (
            <div>
              <h1>{message}</h1>
            </div>
          );
        }
      }

      class HelloWorld extends React.Component {
        constructor(props) {
          super(props);
          this.inputRef = React.createRef();
          this.demosRef = React.createRef();
          this.state = {
            value: "",
          };
        }
        // 这里改成箭头函数,由于箭头函数会继承父类的this,所以这里的this指向实例对象.
        handleSubmt = (e) => {
          // 阻止默认提交行为
          e.preventDefault();
          // 打印ref观察结果
          console.log(this.inputRef.current.value, this.inputRef);
          this.setState({ value: this.inputRef.current.value });
          console.log(this.demosRef.current);
        };
        componentDidMount() {
          console.log(this.demosRef.current);
        }
        render() {
          return (
            <div>
              <Demos ref={this.demosRef} />
              <form action="/abc" onSubmit={this.handleSubmt}>
                <input type="text" ref={this.inputRef} />
                <input type="submit" value="提交" />
              </form>
              <p>输入的值: {this.state.value}</p>
            </div>
          );
        }
      }

      ReactDOM.render(<HelloWorld />, document.getElementById("root"));
    </script>
  </body>
</html>
